// Instantiate a Panel from script
var yuiPanel = function ()
{
    var panel2;
    var txtbody = "No Message included.";
    var txthead = "";
    var txtfoot = "";
    var preX = YAHOO.util.Dom.getViewportWidth() - 100;
    var preY = 30;
    this.setBody = function (text){
        this.txtbody = text;
    };
    this.setHead = function (text){
        this.txthead = text;
    };
    this.setFoot = function (text){
        this.txtfoot = text;
    };
    this.show = function ()
    {
        this.x = parseInt(YAHOO.util.Cookie.get("phpMyPanelX"));
        this.y = parseInt(YAHOO.util.Cookie.get("phpMyPanelY"));
        this.panel2 = new YAHOO.widget.Panel('yuiMsgPan', { draggable:true,
                                                        constraintoviewport: true,
                                                        underlay:"shadow",
                                                        effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.45},
                                                        visible:true,
                                                        zIndex: 1010,
                                                        xy: [this.x ? this.x : preX, this.y ? this.y : preY],
                                                        close:true } );
        if (this.txthead)
            this.panel2.setHeader(this.txthead);
        this.panel2.setBody(this.txtbody);
        if (this.txtfoot)
            this.panel2.setFooter(this.txtfoot);
        var moved = function (type, args)
        {
            if (!args || args == ',') return;
            YAHOO.util.Cookie.set('phpMyPanelX', args[0][0]);
            YAHOO.util.Cookie.set('phpMyPanelY', args[0][1]);
        }
        this.panel2.subscribe('move', moved);
        // this.panel2.subscribe('hide', function () { this.destroy(); } );
        this.panel2.render(document.body);
    };
    this.hide = function ()
    {
        this.panel2.hide();
    };
    this.destroy = function ()
    {
        //this.panel2.destroy();
    };
};
// handle ajax answer
var phpmyhandleSuccess = function(o){
    var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1);
    var data;
    if(json){
        data = eval('(' + json + ')');
    } else data = '';
    //var data = json ? eval('(' + json + ')') : '';
    if (data.Results.MSGBody) {
        var timer = 10; // time until hide
        // show Panel
        var pan = new yuiPanel;
        pan.setBody(data.Results.MSGBody);
        if (data.Results.MSGHead)
            pan.setHead(data.Results.MSGHead);
        if (data.Results.MSGFoot)
            pan.setFoot(data.Results.MSGFoot);
        pan.show();
        timer = parseInt((parseInt(data.Results.MSGTime)>=0 ? data.Results.MSGTime : timer) * 1000)
        if (timer > 0){
            setTimeout(function(){ pan.hide(); }, timer);
        }
    }
    if (data.Results.replaceContentById) {
        // some field values to replace
        for (var Key in data.Results.replaceContentById) {
            if (!Key)
                continue;
            var el = YAHOO.util.Dom.get(Key);
            if (el) {
                el.innerHTML = data.Results.replaceContentById[Key];
            }
            delete el;
        }
    }
    if (data.Results.UpdateValue) {
        // some field values to replace
        for (var Key in data.Results.UpdateValue) {
            if (!Key)
                continue;
            var el = YAHOO.util.Dom.get(Key);
            if (el)
                el.value = data.Results.UpdateValue[Key];
            delete el;
        }
    }
    if (data.Results.js)
        eval(data.Results.js);
};
// handles ajax fail
var phpmyhandleFailure = function(o){
    //alert('XHR-Request failed!');
};
// default call back handler for ajax requests
var phpmycallback = {
    success: phpmyhandleSuccess,
    failure: phpmyhandleFailure
};
/**
 * easy form ajax sender
 * e.g.: stepforminit = new ajaxFormPost({buttonid: 'submitStep', formid: 'stepform', target: '?p=ajax&s=updatestep'});
 *
 * @param object buttonid = button to start submission, formid = form to get datas from, target = xhr target
 *
 */
var ajaxFormPost =  function (vars) {
        var buttonid,formid,target,init = false;
        var obj = this;
        if (vars.buttonid)
            this.buttonid = vars.buttonid;
        if (vars.formid)
            this.formid = vars.formid;
        if (vars.target)
            this.target = vars.target;
        {
            if (this.init) {
                return true;
            }
            this.init = true;
            var _button = new YAHOO.widget.Button(this.buttonid);
            _button.on('click', function () { obj.clicked(); });
        }
        this.clicked = function () { _ajaxSendPostForm(this.target, this.formid); };
};
/**
 * send a form via xhr post
 * @param string xhr target
 * @param string form id to send
 * @param sring datas to add to post submission
 */
var _ajaxSendPostForm = function(target, formid, data){
    var sUrl = target;
    YAHOO.util.Connect.setForm(formid);
    _phpmyXhrPutToStack(YAHOO.util.Connect.asyncRequest('POST', sUrl, phpmycallback, data));
};
/**
 * put a xhr request to the xhr stack
 * @param object xhr object
 * @return void
 */
var _phpmyXhrPutToStack = function (o_xhr){
    phpmyXHRRequestStack.push(o_xhr);
    return;
};
/**
 * cancel all xhr request
 * @return void
 */
var phpmyXhrStopAllRequests = function (){
    for (var i = 0; i < phpmyXHRRequestStack.length; ++i){
        YAHOO.util.Connect.abort(phpmyXHRRequestStack[i]);
    }
    return;
};
 /**
  * send a xhr post
  * @param string xhr target
  * @param sring datas to add to post submission
  */
 var _ajaxSendPost = function(target, data){
     _phpmyXhrPutToStack(YAHOO.util.Connect.asyncRequest('POST', target, phpmycallback, data));
 };
var YDom = YAHOO.util.Dom, YEvent = YAHOO.util.Event, status = null, phpmyXHRRequestStack = new Array();
